<template>
  <div id="app">
    <el-container class="app-container">
      <!-- 侧边栏 -->
      <el-aside width="200px" class="app-aside">
        <div class="logo">
          <img src="@/assets/images/logo.png" alt="系统Logo">
          <h1>Quartz任务管理</h1>
        </div>
        <el-menu
          :default-active="$route.path"
          class="el-menu-vertical-demo"
          @select="handleMenuSelect"
          background-color="#304156"
          text-color="#bfcbd9"
          active-text-color="#409EFF"
          unique-opened
        >
          <el-menu-item index="/dashboard">
            <i class="el-icon-s-home"></i>
            <span slot="title">控制台</span>
          </el-menu-item>
          
          <el-submenu index="/job">
            <template slot="title">
              <i class="el-icon-s-order"></i>
              <span>任务管理</span>
            </template>
            <el-menu-item index="/job/list">任务列表</el-menu-item>
            <el-menu-item index="/job/log">执行日志</el-menu-item>
          </el-submenu>
          
          <el-menu-item index="/about">
            <i class="el-icon-info"></i>
            <span slot="title">关于系统</span>
          </el-menu-item>
        </el-menu>
      </el-aside>
      
      <el-container>
        <!-- 顶部导航 -->
        <el-header class="app-header">
          <div class="header-left">
            <el-button 
              icon="el-icon-menu" 
              size="mini" 
              @click="toggleSidebar"
              class="sidebar-toggle"
            ></el-button>
          </div>
          <div class="header-right">
            <el-dropdown trigger="click">
              <span class="user-info">
                <i class="el-icon-user"></i>
                <span>{{ userName }}</span>
                <i class="el-icon-arrow-down"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item @click="handleLogout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </el-header>
        
        <!-- 主内容区 -->
        <el-main class="app-main">
          <router-view />
        </el-main>
        
        <!-- 页脚 -->
        <el-footer class="app-footer">
          <p>© 2023 Quartz定时任务管理系统 - 版本 v1.0.0</p>
        </el-footer>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'

export default {
  name: 'App',
  data() {
    return {
      sidebarOpened: true
    }
  },
  computed: {
    ...mapGetters(['userName'])
  },
  methods: {
    handleMenuSelect(index) {
      this.$router.push(index)
    },
    toggleSidebar() {
      this.sidebarOpened = !this.sidebarOpened
      const asideEl = document.querySelector('.app-aside')
      asideEl.style.width = this.sidebarOpened ? '200px' : '64px'
    },
    handleLogout() {
      this.$confirm('确定要退出登录吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 清除登录状态
        this.$store.dispatch('user/logout')
        this.$router.push('/login')
        this.$message.success('退出登录成功')
      }).catch(() => {
        // 取消退出
      })
    }
  },
  created() {
    // 初始化用户信息
    this.$store.dispatch('user/getUserInfo')
  }
}
</script>

<style scoped>
.app-container {
  height: 100vh;
  overflow: hidden;
}

.app-aside {
  background-color: #304156;
  transition: width 0.3s;
  height: 100%;
  overflow: hidden;
}

.logo {
  display: flex;
  align-items: center;
  padding: 0 15px;
  height: 60px;
  border-bottom: 1px solid #475669;
}

.logo img {
  width: 36px;
  height: 36px;
  margin-right: 10px;
}

.logo h1 {
  color: #fff;
  font-size: 16px;
  margin: 0;
  white-space: nowrap;
}

.app-header {
  background-color: #fff;
  border-bottom: 1px solid #e6e6e6;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
}

.header-left {
  display: flex;
  align-items: center;
}

.sidebar-toggle {
  margin-right: 15px;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
  padding: 5px 10px;
  border-radius: 4px;
  transition: background-color 0.2s;
}

.user-info:hover {
  background-color: #f5f7fa;
}

.user-info i {
  margin-right: 5px;
}

.app-main {
  padding: 20px;
  overflow-y: auto;
  background-color: #f5f7fa;
}

.app-footer {
  text-align: center;
  padding: 10px 0;
  color: #666;
  font-size: 12px;
  border-top: 1px solid #e6e6e6;
}
</style>
